////////////////////////////////////////////////////////////////////////////////
//  COPYRIGHT (C) 2010 TELECOM DIGITAL MEDIA ("TDMEDIA").
//  ALL RIGHTS RESERVED.
//
//  THIS IS CONFIDENTIAL AND PROPRIETARY INTELLECTUAL PROPERTY OWNED BY AND
//  CREATED ON BEHALF OF TDMEDIA. ANY FORM OF DISTRIBUTION, COPY,
//  MODIFICATION WITHOUT THE WRITTEN CONSENT FROM TDMEDIA IS STRICTLY
//  PROHIBITED.
////////////////////////////////////////////////////////////////////////////////
package cc.telecomdigital.tdstock.activity.groups.more;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import cc.telecomdigital.tdstock.CommonDefn;
import cc.telecomdigital.tdstock.ITDLApplication;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.ActivityGroup_Framework;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Http.H_CODE;
import cc.telecomdigital.tdstock.Http.HttpSuperClassStockBaseActivity;
import cc.telecomdigital.tdstock.Http.Services.HttpAppInfo;
import cc.telecomdigital.tdstock.Http.Services.H_HttpServiceRequest.H_UserClient_HttpRequest_Result;
import cc.telecomdigital.tdstock.Tools.ActivityGroupController;
import cc.telecomdigital.tdstock.activity.groups.MoreGroup;
import cc.telecomdigital.tdstock.activity.groups.StockGroup;
import cc.telecomdigital.tdstock.activity.groups.stock.BMPCurrentStockInfoActivity;
import cc.telecomdigital.tdstock.activity.groups.stock.CurrentStockInfoActivity;
import cc.telecomdigital.tdstock.adapter.ADRListItemAdapter;
import cc.telecomdigital.tdstock.adapter.ADRViewHolder;

/**
 * 
 * @author haibo
 * 
 *  for  list item on ShortADR
 *
 */
public class StockADRActivity extends HttpSuperClassStockBaseActivity
{
	protected static final String	TAG			= StockADRActivity.class.getSimpleName();
	private static final long		START_ID	= 90000l;

	private ListView				_listView;
	private Button					_toolBtnReturn;

	private static ADRListItemAdapter	_adrAdapter;
	private static List<ADRViewHolder>	_adrList;
	private SharedPreferences		_prefsStockCode;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.more_stock_adr_content);

		_toolBtnReturn = (Button) findViewById(R.id.more_returnbtn);
		_toolBtnReturn.setFocusable(true);
		_toolBtnReturn.setClickable(true);
		_toolBtnReturn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				TDStockLog.d(TAG, "close current activity and forwarding to the previous");

				Intent intent = new Intent(getApplicationContext(), MoreActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NO_USER_ACTION);
				SwitchForwardActivity(MoreActivity.class, intent);
			}
		});

		_messageHandler = null; //this activity no need it.
		_listView = (ListView) findViewById(R.id.more_stock_adr_list);

		_adrList = new ArrayList<ADRViewHolder>();
		_adrAdapter = new ADRListItemAdapter(LayoutInflater.from(this), _adrList);

		_listView.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id)
			{
				//the first line is the title
				if (position != -1 && _adrList.size() >= position + 1)
				{
					// modify by zor(ouyangzhihui)
					String tmp = _adrList.get(position).itemCode.getText()
							.toString();
					String stockCode = "";
					if (!"".equals(tmp))
					{
						stockCode = String.valueOf(Integer.parseInt(tmp));
					}
					_prefsStockCode = getSharedPreferences(
							CommonDefn.SYSTEM_CURRENT_SETTING,
							Context.MODE_PRIVATE);
					_prefsStockCode.edit().putString(
							CommonDefn.PREFERENCE_STOCK_CODE, stockCode)
							.commit();

					// BMP Mode  by ouyangzhihui 2011-10-13
					Class<? extends ActivityGroup_Framework>_cls = CurrentStockInfoActivity.class;
					if(((ITDLApplication)getApplicationContext()).getIsBMPMode())
					{
						_cls = BMPCurrentStockInfoActivity.class;
					}
					
					Intent intent = new Intent(getApplicationContext(), _cls);
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NO_USER_ACTION);
					SwitchTabHostActivity(0, StockGroup.GetActivityGroupHostControl(), _cls,intent);
				}
			}
		});

		_listView.setAdapter(_adrAdapter);
	}

	@Override
	protected void onResume()
	{
		SetExectuHttpRequestOnResume(false);
		
		super.onResume();
		
		if (((ITDLApplication) getApplicationContext()).getDataMode() == CommonDefn.DataMode.Delay)
		{
			_adrList.clear();

			ADRViewHolder holder = new ADRViewHolder();
			holder.id = START_ID;
			holder.itemCode = new TextView(this);
			holder.itemName = new TextView(this);
			holder.price = new TextView(this);
			holder.net = new TextView(this);
			holder.itemCode.setText("");
			holder.itemName.setText(getResources().getString(R.string.delay_mode_chargeable_data_tips));
			holder.price.setText("");
			holder.net.setText("");
			_adrList.add(holder);

			_adrAdapter.notifyDataSetChanged();
			return;
		}
		SendHttpRequest();
		_listView.invalidateViews();		
	}
	
	private void resetContent()
	{
		_adrList.clear();
		_adrAdapter.notifyDataSetChanged();
	}
	
	@Override
	public ActivityGroupController GetActivityGroupController()
	{
		return MoreGroup.GetActivityGroupHostControl();
	}

	private void updateData(List<String[]> result)
	{
		_adrList.clear();
		int index = 0;
		long id = START_ID + _adrList.size();
		for (String[] array : result)
		{
			if (array[0] == null || array[0].length() < 1)
			{
				continue;
			}

			ADRViewHolder holder = new ADRViewHolder();
			holder.id = id + index;
			holder.itemCode = new TextView(StockADRActivity.this);
			holder.itemName = new TextView(StockADRActivity.this);
			holder.price = new TextView(StockADRActivity.this);
			holder.net = new TextView(StockADRActivity.this);

			holder.itemCode.setText(String.format("%05d", Integer.parseInt(array[0])));
			holder.itemName.setText(array[1]);
			holder.price.setText(array[2]);
			holder.net.setText(array[3]);

			_adrList.add(holder);
			index++;
		}
		_adrAdapter.notifyDataSetChanged();
	}
	
	@Override
	protected H_CODE GetDataFromHttpServer()
	{
		resetContent();
		return HttpAppInfo.SendADRRequest(actContext, this);
	}

	@Override
	protected void H_UserClient_HttpRequest_Response_Sub(final H_UserClient_HttpRequest_Result iResult)
	{
		if(!iResult.MStatus.equals("0"))
		{
			ShowMessageDialog(iResult.MError+"("+iResult.MStatus+")");
			return;
		}
		
		Runnable runnable = new Runnable()
		{
			@Override
			public void run()
			{
				updateData(iResult.contentStores);
			}
		};
		_messageHandler.post(runnable);
	}
}
